package com.gitee.wsl.common.ui.input

import com.gitee.wsl.common.ui.input.event.EventPropagation
import com.gitee.wsl.common.ui.input.event.KEvent


/**
 * TODO: rename with a more explicit name.
 */
fun interface Disposable {

    /**
     * Remove the event listener from the Viz.
     * TODO: rename with a more explicit name.
     */
     fun dispose()
}

class KEventHandle<T>(
    val eventListener: KEventListener<T>,
    val listener: (T) -> EventPropagation,
    val onDispose: (KEventHandle<T>) -> Unit
) : Disposable where T : KEvent {

    var disposable: Disposable? = null

    val isAddedToRenderer
        get() = disposable != null

    override fun dispose() {
        onDispose(this)
    }

    override fun toString(): String = "KEventHandle(eventListener=$eventListener)"
}

internal class CompositeDisposable(val disposables: MutableList<Disposable> = mutableListOf()) : Disposable {
    override fun dispose() {
        disposables.forEach { it.dispose() }
        disposables.clear()
    }

    fun add(disposable: Disposable) {
        disposables.add(disposable)
    }
}